﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Windows Forms Controls Class Library
 * Version 2.00
 * 
 * Toolbox Title Control Class
 *      Provides a UI element for displaying a toolbox title bar.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Adaptive.Foundation.Windows.Forms
{
    /// <summary>
    /// Provides a UI element for displaying a toolbox title bar.
    /// </summary>
    [ToolboxItem(false)]
    public partial class ToolboxTitleControl : TooboxTitleControlBase
    {
        /*--------------------------------------------------------------------------------
         * Public Event Definitions
         *------------------------------------------------------------------------------*/
        #region Public Event Definitions
        /// <summary>
        /// Occurs when the close button is clicked.
        /// </summary>
        public event CancelEventHandler ToolboxCloseRequest;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ToolboxTitleControl()
            : base()
        {
            TextOffset = new Point(0, 0);
            MinimumSize = new Size(0, 18);
            Height = 18;

            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.ContainerControl, false);
            SetStyle(ControlStyles.Opaque, false);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.Selectable, false);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.UserPaint, true);

            StartColor = SystemColors.GradientActiveCaption;
            MidColor = SystemColors.ActiveCaption;
            ForeColor = SystemColors.ActiveCaptionText;

            if (Width == 0)
                Width = 1;

            SetColors();
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets a value indicating whether the close button is displayed.
        /// </summary>
        /// <value>
        /// <b>true</b> to show the close button; otherwise, <b>false</b>.
        /// </value>
        [Browsable(true),
         DefaultValue(true),
         Description("Show or hide the close button.")]
        public bool ShowCloseButton
        {
            get { return CloseButton.Visible; }
            set
            {
                CloseButton.Visible = value;
                Invalidate();
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Creates and assigns the event handlers for the items contained within the control.
        /// </summary>
        protected override void AssignEventHandlers()
        {
            CloseButton.Click +=new System.EventHandler(HandleCloseClick);
        }
        /// <summary>
        /// Removes the event handlers for the items contained within the control.
        /// </summary>
        protected override void RemoveEventHandlers()
        {
            CloseButton.Click -=new System.EventHandler(HandleCloseClick);
        }
        /// <summary>
        /// Raises the <see cref="ToolboxCloseRequest"/> event.
        /// </summary>
        /// <param name="e">
        /// An <see cref="CancelEventArgs"/> event arguments instance containing
        /// a flag for cancelling the operation.
        /// </param>
        protected virtual void OnToolboxCloseRequest(CancelEventArgs e)
        {
            if (ToolboxCloseRequest != null)
                ToolboxCloseRequest(this, e);
        }
        #endregion

        /// <summary>
        /// Handles the event when the close button is clicked.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="E:System.EventArgs"/> event arguments instance.</param>
        private void HandleCloseClick(object sender, System.EventArgs e)
        {
            CancelEventArgs evArgs = new CancelEventArgs();

            evArgs.Cancel = false;
            OnToolboxCloseRequest(evArgs);
        }
    }
}
